const commentService = require('../service/comment.service')

class CommentController {
  // 发表评论
  async create(ctx, next) {
    const {momentId, content} = ctx.request.body;
    const userId = ctx.user.id
    try {
      const result = await commentService.create(userId, momentId, content)      
      if(result) {
        ctx.success({
          msg: '发表成功'
        })
      }
    } catch (error) {
      console.log(error.message)
      return
    }
  }

  // 回复评论
  async reply(ctx, next) {
    const {momentId, content} = ctx.request.body;
    const userId = ctx.user.id;
    const {commentId} = ctx.params
    try {
      const result = await commentService.reply(userId, momentId, commentId, content)
      if(result) {
        ctx.success({
          msg: '回复成功'
        })
      }
    } catch (error) {
      console.log(error.message)
      return
    }
  }

  // 修改评论
  async update(ctx, next) {
    const {content} = ctx.request.body
    const {commentId} = ctx.params
    try {
      const result = await commentService.update(commentId, content)
      if(result) {
        ctx.success({
          msg: '修改成功'
        })
      }
    } catch (error) {
      console.log(error.message)
      return
    }
  }

  // 删除评论
  async remove(ctx, next) {
    const {commentId} = ctx.params
    ctx.body = {commentId};
    try {
      const result = await commentService.remove(commentId)
      if(result) {
        ctx.success({
          msg: '删除成功'
        })
      }
    } catch (error) {
      console.log(error.message)
      return
    }
  }

  // 获取评论列表
  async list(ctx, next) {
    const {momentId} = ctx.query;
    try {
      const result = await commentService.getListById(momentId)
      if(result) {
        ctx.success({
          data: result
        })
      }
    } catch (error) {
      console.log(error.message)      
      return
    }
  }

  // 测试
  async test(ctx, next) {
    const username = '柯南'
    console.log('ehllo')
    const result = await commentService.test(username)
  }
}

module.exports = new CommentController()